package com.phison.fenestrapro.dashboard.util;

import com.phison.fenestrapro.dashboard.data.Project;
import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Field;
import com.vaadin.ui.Form;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Layout;
import com.vaadin.ui.VerticalLayout;

/**
 *
 * 
 * @author Niall O'Hara
 *
 */
public class FormBuilder implements GlobalConstants {

	private Project building;
	private BeanItem<Project> buildingItem;
	
	public FormBuilder(Project building, BeanItem<Project> buildingItem) {
		this.building = building;
		this.buildingItem = buildingItem;
	}

	@SuppressWarnings("serial")
	public Layout buildFormLayout(Object[] fields, int layout) {

		Layout root = null;
		
		if (layout == vertical)
			root = new VerticalLayout();
		
		if (layout == horizontal)
			root = new HorizontalLayout();
		
		if (layout == grid) {
			root = new GridLayout();
			root.setWidth("100%");
		}
		
		final Form formContents = new Form() {
			
			FormLayout layout = new FormLayout();

			{
				setImmediate(true);
				setWriteThrough(true);
				layout.setSpacing(true);
				layout.setMargin(true, false, false, true);
				layout.setWidth("95%");
				setLayout(layout);
			}

			@Override
			protected void attachField(Object propertyId, Field field) {
				if ("comment".equals(propertyId)) {
					// A two-column field
				} else {// Single-column fields
					layout.addComponent(field);
				}
			}
			
		};

		formContents.setFormFieldFactory(new MyFieldFactory(building));

		formContents.setItemDataSource(buildingItem);

		formContents.setVisibleItemProperties(fields);

		root.addComponent(formContents);

		return root;

	}

}
